<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-10-18 14:03:51
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Comprehensive;


use App\Http\Services\Service;
use App\Repositories\ComprehensiveCarryDetailsRepository;
use App\Repositories\ComprehensiveCarryRepository;


class CarryService extends Service
    {
    public $details;
    public function __construct(ComprehensiveCarryRepository $repository, ComprehensiveCarryDetailsRepository $details)
        {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function details()
        {
        $data = $this->details->condition(request())->getList();
        return $data;
        }
    protected function save($request)
        {
        $data  = $request->toArray();
        $gdata = $this->repository->whereId($data['id'] ?? 0)->first();
        if ($gdata && $gdata->status_id != 0) {
            fail("单据【{$gdata->code}】【{$gdata->status->name}】,保存失败！");
            }
        $data['quantity'] = $data['auxiliary_quantity'] = 0;
        array_filter($data['details'], function ($v) use (&$data)
            {
            $data['quantity']           = $data['quantity'] + $v['quantity'];
            $data['auxiliary_quantity'] = $data['auxiliary_quantity'] + $v['auxiliary_quantity'];
            });
        return $this->repository->autosave($data);
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("单据状态【{$v->status->name}】,反审核失败！");
                }
            auditSave($v);
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 1) {
                fail("单据状态【{$v->status->name}】,反审核失败！");
                }
            auditSave($v, false);
            });
        }
    protected function delete($request)
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id == 1)
                fail("单据已审核！");
            $v->details->filter(function ($vv)
                {
                $vv->delete();
                });
            $v->delete();
            });
        }
    protected function saveAmount()
        {
        $request = request();
        return array_map(function ($v)
            {
            $data = $this->repository->whereId($v['id'])->first();
            if ($data->status_id != 0) {
                fail("单据状态【{$data->status->name}】,修改失败！");
                }
            $data->amount = $v['amount'];
            return $data->save();
            }, $request->updateRecords);
        }
    protected function deleteItem()
        {
        $this->details->whereIn("id", $this->getRequestIds())->get()->filter(function ($detail)
            {
            if ($detail->main->status_id != 0) {
                fail("派车单【{$detail->main->code}】已【{$detail->main->status->name}】,删除失败！");
                }
            $detail->delete();
            });
        }
    protected function faudit()
        {
        $financeremark = request('financeremark') ?? "";
        $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v) use ($financeremark)
            {
            if ($v->status_id != 1) {
                fail("单据【{$v->code}】状态【{$v->status->name}】,审核失败！");
                }
            $v->financeremark = $financeremark;
            return $v->finance();
            });
        }
    }